'use strict'

import { app, BrowserWindow, ipcMain } from 'electron'
import fs from 'fs'
import request from 'request'

/**
 * Set `__static` path to static files in production
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html
 */
if (process.env.NODE_ENV !== 'development') {
  global.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
}

let mainWindow
const winURL = process.env.NODE_ENV === 'development'
  ? `http://localhost:9080`
  : `file://${__dirname}/index.html`

function createWindow () {
  /**
   * Initial window options
   */
  const splash = new BrowserWindow({
    width: 800,
    height: 600,
    frame: false,
    icon: global.__static + '/logo.png'
  })
  setTimeout(function () {
    mainWindow = new BrowserWindow({
      height: 563,
      useContentSize: true,
      width: 1000,
      webPreferences: {
        webSecurity: false
      },
      icon: global.__static + '/logo.png'
    })
    splash.close()
    mainWindow.loadURL(winURL)

    mainWindow.on('closed', () => {
      mainWindow = null
    })
  }, 2000)
}

ipcMain.on('download', function (event, arg) {
  request({
    url: arg.down,
    methods: 'GET',
    headers: {
      'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.81 Safari/537.36',
      'Referer': 'http://image.baidu.com/search/index?tn=baiduimage&ct=201326592&lm=-1&cl=2&ie=gb18030&word=%CD%BC%C6%AC&fr=ala&ala=1&alatpl=others&pos=0'
    },
    encoding: null
  }, function (error, response, body) {
    if (!error && response.statusCode === 200) {
      // console.log(body) // 请求成功的处理逻辑
      fs.writeFile(arg.name, body, function (err) {
        if (err) {
          throw err
        }
        // mainWindow.webContents.send('downComplete', arg.name)
        event.sender.send('downComplete', arg.name)
      })
    }
  })

  // fs.writeFile(arg.name, Buffer.from(arg.src, 'base64'), function (err) {
  //   if (err) {
  //     throw err
  //   }
  // mainWindow.webContents.send('downComplete', arg.name)
  // event.sender.send('downComplete', arg.name)
  // dialog.showMessageBox({
  //   title: '完成',
  //   type: 'error',
  //   message: arg.name
  // })
  // })
  // mainWindow.webContents.downloadURL(arg.url)
})

app.on('ready', createWindow)

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', () => {
  if (mainWindow === null) {
    createWindow()
  }
})

/**
 * Auto Updater
 *
 * Uncomment the following code below and install `electron-updater` to
 * support auto updating. Code Signing with a valid certificate is required.
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-electron-builder.html#auto-updating
 */

/*
import { autoUpdater } from 'electron-updater'

autoUpdater.on('update-downloaded', () => {
  autoUpdater.quitAndInstall()
})

app.on('ready', () => {
  if (process.env.NODE_ENV === 'production') autoUpdater.checkForUpdates()
})
 */
